package com.rybbaby.tss.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

/**
 * @description 处理开发者参考页面
 * 
 * @author 谢进伟
 * 
 * @createDate 2016-1-28 下午7:02:31
 */
public class DisposeDevelopmentFile {
	
	private static List<String> baseFileList = new ArrayList<String>();
	private static String developmentDir = "G:/WorkSpace/MyEclipse/MyEclipse10.7_wechat/wmp/src/main/webapp/WEB-INF/views/development";
	
	static {
		// 框架已导入的在的脚本文件
		baseFileList.add("assets/js/skins.min.js");
		baseFileList.add("assets/js/jquery-2.0.3.min.js");
		baseFileList.add("assets/js/bootstrap.min.js");
		baseFileList.add("assets/js/resize/jquery.element.resize.js");
		baseFileList.add("assets/js/WdatePicker/WdatePicker.js");
		baseFileList.add("assets/js/datetime/moment.js");
		baseFileList.add("assets/js/datetime/daterangepicker.js");
		baseFileList.add("assets/js/beyond.min.js");
		baseFileList.add("assets/js/charts/sparkline/jquery.sparkline.js");
		baseFileList.add("assets/js/charts/sparkline/sparkline-init.js");
		baseFileList.add("assets/js/charts/easypiechart/jquery.easypiechart.js");
		baseFileList.add("assets/js/charts/easypiechart/easypiechart-init.js");
		baseFileList.add("assets/js/charts/flot/jquery.flot.js");
		baseFileList.add("assets/js/charts/flot/jquery.flot.resize.js");
		baseFileList.add("assets/js/charts/flot/jquery.flot.pie.js");
		baseFileList.add("assets/js/charts/flot/jquery.flot.tooltip.js");
		baseFileList.add("assets/js/charts/flot/jquery.flot.orderBars.js");
		// 框架已导入的在的样式文件
		baseFileList.add("assets/img/favicon.png");
		baseFileList.add("assets/css/bootstrap.min.css");
		baseFileList.add("assets/css/font-awesome.min.css");
		baseFileList.add("assets/css/weather-icons.min.css");
		baseFileList.add("assets/css/footer.css");
		baseFileList.add("assets/fonts/DXI1ORHCpsQm3Vp6mXoaTRampu5_7CjHW5spxoeN3Vs.woff2");
		baseFileList.add("assets/css/beyond.min.css");
		baseFileList.add("assets/css/custom.min.css");
		baseFileList.add("assets/css/typicons.min.css");
		baseFileList.add("assets/css/animate.min.css");
		baseFileList.add("assets/css/skins/deepblue.min.css");
		baseFileList.add("assets/js/WdatePicker/skin/WdatePicker.css");
	}
	
	public static void main(String [] args) {
		File devlopmentDir = new File(developmentDir);
		File [] listFiles = devlopmentDir.listFiles();
		for(File file : listFiles) {
			if(file.getName().toLowerCase().contains("_new_")) {
				continue;
			}
			StringBuffer html = new StringBuffer();
			StringBuffer scripts = new StringBuffer();
			redFile(file , html , scripts);
			String htmlStr = html.toString();
			try {
				String pageBody = "<div class=\"page-body\">";
				if(htmlStr.contains(pageBody)) {
					html.replace(0 , html.indexOf(pageBody) + pageBody.length() , "");
				} else {
					pageBody = "<div class=\"page-body no-padding\">";
					if(htmlStr.contains(pageBody)) {
						html.replace(0 , html.indexOf(pageBody) + pageBody.length() , "");
					}
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			try {
				if(htmlStr.contains("<!-- /Page Body -->")) {
					html.replace(html.lastIndexOf("<!-- /Page Body -->") , html.lastIndexOf("<!-- Main Container -->") , "");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			removeLine(html , "</div>");
			removeLine(html , "</div>");
			removeLine(html , "</body>");
			removeLine(html , "</html>");
			removeLine(html , "<!--  /Body -->");
			removeLine(html , "<!-- Main Container -->");
			writerFile(file , html , scripts);
		}
	}
	
	/**
	 * @param html
	 *            移除多余行
	 * @param tag
	 */
	private static void removeLine(StringBuffer html , String tag) {
		try {
			if(html.toString().contains(tag)) {
				html.replace(html.lastIndexOf(tag) , html.lastIndexOf(tag) + tag.length() , "");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 创建新文件
	 * 
	 * @param file
	 *            原始文件
	 * @param html
	 *            新文件中的html及导入的样式内容
	 * @param scripts
	 *            新文件的脚本文件
	 */
	private static void writerFile(File file , StringBuffer html , StringBuffer scripts) {
		OutputStream os = null;
		try {
			File newFile = new File(file.getParent() + File.separator + "_new_" + file.getName());
			System.out.println("正在创建/覆盖文件：" + newFile.getPath());
			if(!newFile.exists()) {
				newFile.delete();
			}
			newFile.createNewFile();
			os = new FileOutputStream(newFile);
			os.write(html.toString().getBytes());
			os.write(scripts.toString().getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(os != null) {
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 读取并处理文件
	 * 
	 * @param file
	 *            需要读取的文件
	 * @param sb
	 *            从file文件中读取的html及样式内容
	 * @param scripts
	 *            从file文件中读取的的脚本内容
	 */
	private static void redFile(File file , StringBuffer sb , StringBuffer scripts) {
		InputStream is = null;
		Reader reader = null;
		BufferedReader br = null;
		try {
			is = new FileInputStream(file);
			reader = new InputStreamReader(is);
			br = new BufferedReader(reader);
			String lineStr = "";
			boolean isScript = false;
			while ((lineStr = br.readLine()) != null) {
				if(lineStr.contains("<link href=\"http://fonts.useso.com/css?")) {// 获取获取字体的导入样式
					continue;
				}
				if(lineStr.toLowerCase().matches("^\\s*<script\\s+src=\".*\".*>\\s*</script>\\s*$")) {
					String src = StringUtils.substringBetween(lineStr , "src=\"" , "\"");
					if(baseFileList.contains(src)) {
						continue;
					}
				}
				if(lineStr.toLowerCase().matches("^\\s*<script\\s*(type=.*)?>\\s*$")) {
					isScript = true;
				}
				if(isScript) {
					if(lineStr.toLowerCase().matches("^\\s*</script>\\s*$")) {
						isScript = false;
					}
					if(lineStr.toLowerCase().contains("$(window).bind(\"load\", function () {")) {
						lineStr = "$(function(){";
					}
					scripts.append(lineStr + "\n");
				} else {
					if(lineStr.matches("^\\s{20}.*$")) {
						lineStr = lineStr.substring(20);
					}
					sb.append(lineStr + "\n");
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(br != null) {
					br.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Test
	public void test() {
		String str = "<script src=\"assets/js/beyond.min.js\"></script>";
		System.out.println(str.matches("^\\s*<script\\s+src=\".*\".*>\\s*</script>\\s*$"));
		System.out.println("<script>".matches("^\\s*<script\\s*(type=.*)?>\\s*$"));
		System.out.println("    ".length());
	}
}
